﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

using GalaSoft.MvvmLight.Threading;
using GalaSoft.MvvmLight.Views;
using System.Windows.Threading;
using MODEL;
using GalaSoft.MvvmLight.Ioc;
using msap.Common;
using Common;

namespace msap
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            Current.DispatcherUnhandledException += App_OnDispatcherUnhandledException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            SimpleIoc.Default.Register<IDialogService, WPFDialogService>();
            SimpleIoc.Default.Register<IWaitingService, WPFWaitingService>();
            DispatcherHelper.Initialize();
            AppSetting.Instance.Initialize();   
        }

        /// <summary>
        /// UI线程抛出全局异常事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void App_OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            e.Handled = true;
            SimpleIoc.Default.GetInstance<IDialogService>().ShowError(string.Format("我们很抱歉,系统出现了异常:\n{0}", e.Exception.Message), "系统异常", null, null);
        }

        /// <summary>
        /// 非UI线程抛出全局异常事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception exception = e.ExceptionObject as Exception;
            SimpleIoc.Default.GetInstance<IDialogService>().ShowError(exception.Message, "错误", null, null);
        }
    }
}
